package com.bkgy.modular.tbpaopianpdidata.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.tbpaopianpdidata.dao.TbPaopianPdiDataDao;
import com.bkgy.modular.tbpaopianpdidata.entity.ExportPaoPianDataNew;
import com.bkgy.modular.tbpaopianpdidata.entity.JTbPaopianPdiData;
import com.bkgy.modular.tbpaopianpdidata.entity.PaoPianDataNew;
import com.bkgy.modular.tbpaopianpdidata.entity.TbPaopianPdiData;
import com.bkgy.modular.tbpaopianpdidata.service.TbPaopianPdiDataService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CodiUtil;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 连退跑偏pdi接口具体实现类
* @author  生成器生成
* @date 2022-08-10 09:11:38
*/
@Service
public class TbPaopianPdiDataServiceImpl implements TbPaopianPdiDataService {
	
	@Autowired
	private TbPaopianPdiDataDao tbPaopianPdiDataDao;
	
	/**
     * 添加记录
     * @param data json字符串
     * @param userId 用户id
     * @param sname 用户姓名
     */
	public ResultData insertDataTbPaopianPdiData(String data,Long userId,String sname){
		try{
			JTbPaopianPdiData jtbPaopianPdiData = JSON.parseObject(data,JTbPaopianPdiData.class);
    		TbPaopianPdiData tbPaopianPdiData = jtbPaopianPdiData.getTbPaopianPdiData();
    		CodiUtil.newRecord(userId,sname,tbPaopianPdiData);
            tbPaopianPdiDataDao.insertDataTbPaopianPdiData(tbPaopianPdiData);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
    	}catch(Exception e){
    		e.printStackTrace();
    		return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null); 
    	}
	};
	
	/**
	 * 根据主键删除对象
	 * @param indocno  对象主键
	 * */
	public ResultData deleteDataTbPaopianPdiDataOne(Long indocno){
		try {
            tbPaopianPdiDataDao.deleteDataTbPaopianPdiDataOne(indocno);
             return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 根据主键字符串删除多个对象
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbPaopianPdiDataMany(String str_id) {
        try {
        	String sql = "delete tb_paopian_pdi_data where indocno in(" + str_id +")";
            tbPaopianPdiDataDao.deleteDataTbPaopianPdiDataMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
     * 修改记录
     * @param userId 用户id
     * @param sname 用户姓名
     * @param data sysUser 对象实体
     */
	public ResultData updateDataTbPaopianPdiData(String data,Long userId,String sname){
		try {
			JTbPaopianPdiData jtbPaopianPdiData = JSON.parseObject(data,JTbPaopianPdiData.class);
    		TbPaopianPdiData tbPaopianPdiData = jtbPaopianPdiData.getTbPaopianPdiData();
        	CodiUtil.editRecord(userId,sname,tbPaopianPdiData);
            tbPaopianPdiDataDao.updateDataTbPaopianPdiData(tbPaopianPdiData);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 分页查看记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbPaopianPdiDataByPage(String data) {
        try {
        	JTbPaopianPdiData jtbPaopianPdiData = JSON.parseObject(data, JTbPaopianPdiData.class);
        	Integer pageIndex = jtbPaopianPdiData.getPageIndex();
        	Integer pageSize = jtbPaopianPdiData.getPageSize();
        	
        	if(null == pageIndex || null == pageSize){
        		return ResultData.ResultDataFaultSelf("分页参数没有传", null); 
        	}
			// 获取查询条件
			JSONObject jsonData = JSON.parseObject(jtbPaopianPdiData.getCondition().toString());
			List<PaoPianDataNew> list = tbPaopianPdiDataDao.findDataTbPaopianPdiDataByPage((pageIndex-1)*pageSize, pageIndex * pageSize,jsonData);
    		Integer count = tbPaopianPdiDataDao.findDataTbPaopianPdiDataByPageSize(jsonData);
    		return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
    
    /**
     * 根据主键查询单条记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbPaopianPdiDataByIndocno(String data) {
        try {
        	JTbPaopianPdiData jtbPaopianPdiData = JSON.parseObject(data, JTbPaopianPdiData.class); 
        	Long indocno = jtbPaopianPdiData.getIndocno();
        	
    		TbPaopianPdiData tbPaopianPdiData = tbPaopianPdiDataDao.findDataTbPaopianPdiDataByIndocno(indocno);
    		return ResultData.ResultDataSuccess(tbPaopianPdiData);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
	 * 查看记录
	 * @return list 对象集合返回
	 * */
	public List<TbPaopianPdiData> findDataTbPaopianPdiData(){
		List<TbPaopianPdiData> list = tbPaopianPdiDataDao.findDataTbPaopianPdiData();
		return list;
	}

	/**
	 * 根据卷号查看记录
	 */
	@Override
	public ResultData findCustMatNoData(JSONObject json) {
		Integer pageIndex = json.getInteger("pageIndex");
		Integer pageSize = json.getInteger("pageSize");
		JSONObject condition = json.getJSONObject("condition");

		if(null == pageIndex || null == pageSize){
			return ResultData.ResultDataFaultSelf("分页参数没有传", null);
		}

		List<PaoPianDataNew> custMatNoDataByPage = tbPaopianPdiDataDao.findCustMatNoDataByPage((pageIndex - 1) * pageSize,pageSize * pageIndex,condition);
		Integer count = tbPaopianPdiDataDao.findCustMatNoDataByPageSize(condition);
		return ResultData.ResultDataSuccess(custMatNoDataByPage,count);
	}

	/**
	 * 页面导出功能
	 * @param jsonObject
	 * @param response
	 */
	@Override
	public void export(JSONObject jsonObject, HttpServletResponse response) {
		// 接收传入参数
		Integer pageSize = jsonObject.getInteger("pageSize");
		Integer pageCount = jsonObject.getInteger("pageCount");
		String fileNames = jsonObject.getString("fileName");
		String exportType = jsonObject.getString("exportType");
		JSONObject condition = jsonObject.getJSONObject("condition");
		// 创建表格数据集合
		List<PaoPianDataNew> listExcel = new ArrayList<>();

		// 调用service查询出数据
		List<PaoPianDataNew> data = null;
		if ("1".equals(exportType)) {
			for (int i = 1; i <= pageCount; i++) {
				// 分页去数据库查询数据 这里可以去数据库查询每一页的数据
				data = tbPaopianPdiDataDao.findCustMatNoDataByPage((i - 1) * pageSize, i * pageSize, condition);
				for (PaoPianDataNew item : data) {
					listExcel.add(item);
				}
			}
		} else if ("2".equals(exportType)) {
			for (int i = 1; i <= pageCount; i++) {
				// 分页去数据库查询数据 这里可以去数据库查询每一页的数据
				data = tbPaopianPdiDataDao.findDataTbPaopianPdiDataByPage((i - 1) * pageSize, i * pageSize, condition);
				for (PaoPianDataNew item : data) {
					listExcel.add(item);
				}
			}
		}
		// 判断service查询出的数据集合data是否为空
		if (CollectionUtils.isEmpty(listExcel)) {
			return;
		}
		List<ExportPaoPianDataNew> list = new ArrayList<>();
		if (!listExcel.isEmpty() && listExcel.size() > 0) {
			for (PaoPianDataNew item : listExcel) {
				ExportPaoPianDataNew exportPaoPianDataNew = new ExportPaoPianDataNew();
				BeanUtils.copyProperties(item,exportPaoPianDataNew);
				list.add(exportPaoPianDataNew);
			}
		}


		String classpath="";
		if(fileNames.equals("生产实绩跑偏查询"))
		{
			classpath="classpath:exceltemplate\\生产实绩跑偏查询.xlsx";
		}
		if(fileNames.equals("生产计划"))
		{
			classpath="classpath:exceltemplate\\生产计划.xlsx";
		}
		try {
			Map<String,Object> data1 = new HashMap();
			data1.put("data",list);
			File file = ResourceUtils.getFile(classpath);
			TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
			templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
			Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
			ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		//ExcelUtils.downloadExcel(fileNames,response, PaoPianDataNew.class,listExcel);
	}
}
